using System.Web.Http;
using Ninject.Web.Mvc;
using xConnected.Core.Interfaces.Data;
using xConnected.Core.Interfaces.Service;
using xConnected.Data;
using xConnected.Service;

[assembly: WebActivator.PreApplicationStartMethod(typeof(xConnected.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(xConnected.App_Start.NinjectWebCommon), "Stop")]

namespace xConnected.App_Start
{
    using System;
    using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;

    public static class NinjectWebCommon 
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }
        
        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }
        
        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
            
            RegisterServices(kernel);

            // Install our Ninject-based IDependencyResolver into the Web API config
            //GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);

            return kernel;
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IDatabaseFactory>().To<DatabaseFactory>().InRequestScope();
            kernel.Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();
            kernel.Bind<ICategoryService>().To<CategoryService>().InRequestScope();
            kernel.Bind<ICategoryRepository>().To<CategoryRepository>().InRequestScope();
            kernel.Bind<IProfileService>().To<ProfileService>().InRequestScope();
            kernel.Bind<IProfileRepository>().To<ProfileRepository>().InRequestScope();
            kernel.Bind<IPositionService>().To<PositionService>().InRequestScope();
            kernel.Bind<IPositionRepository>().To<PositionRepository>().InRequestScope();
            kernel.Bind<ISkillService>().To<SkillService>().InRequestScope();
            kernel.Bind<ISkillRepository>().To<SkillRepository>().InRequestScope();
            kernel.Bind<IUserService>().To<UserService>().InRequestScope();
            kernel.Bind<IUserRepository>().To<UserRepository>().InRequestScope();
            kernel.Bind<IExpertProfileService>().To<ExpertProfileService>().InRequestScope();
            kernel.Bind<IExpertProfileRepository>().To<ExpertProfileRepository>().InRequestScope();
            kernel.Bind<ICompanyProfileService>().To<CompanyProfileService>().InRequestScope();
            kernel.Bind<ICompanyProfileRepository>().To<CompanyProfileRepository>().InRequestScope();
            kernel.Bind<ILanguageService>().To<LanguageService>().InRequestScope();
            kernel.Bind<ILanguageRepository>().To<LanguageRepository>().InRequestScope();
            kernel.Bind<ILanguageSkillService>().To<LanguageSkillService>().InRequestScope();
            kernel.Bind<ILanguageSkillRepository>().To<LanguageSkillRepository>().InRequestScope();
            kernel.Bind<ICountryService>().To<CountryService>().InRequestScope();
            kernel.Bind<ICountryRepository>().To<CountryRepository>().InRequestScope();
            kernel.Bind<ITechnologyService>().To<TechnologyService>().InRequestScope();
            kernel.Bind<ITechnologyRepository>().To<TechnologyRepository>().InRequestScope();
            kernel.Bind<ITechnologySkillService>().To<TechnologySkillService>().InRequestScope();
            kernel.Bind<ITechnologySkillRepository>().To<TechnologySkillRepository>().InRequestScope();
            kernel.Bind<IEducationService>().To<EducationService>().InRequestScope();
            kernel.Bind<IEducationRepository>().To<EducationRepository>().InRequestScope();
            kernel.Bind<IIndustryService>().To<IndustryService>().InRequestScope();
            kernel.Bind<IIndustryRepository>().To<IndustryRepository>().InRequestScope();
            kernel.Bind<IEducationSkillService>().To<EducationSkillService>().InRequestScope();
            kernel.Bind<IEducationSkillRepository>().To<EducationSkillRepository>().InRequestScope();
            kernel.Bind<ICategorySkillService>().To<CategorySkillService>().InRequestScope();
            kernel.Bind<ICategorySkillRepository>().To<CategorySkillRepository>().InRequestScope();
            kernel.Bind<IMessageService>().To<MessageService>().InRequestScope();
            kernel.Bind<IMessageRepository>().To<MessageRepository>().InRequestScope();
            kernel.Bind<IMessageFolderService>().To<MessageFolderService>().InRequestScope();
            kernel.Bind<IMessageFolderRepository>().To<MessageFolderRepository>().InRequestScope();
            kernel.Bind<IMessageExpertUserService>().To<MessageExpertUserService>().InRequestScope();
            kernel.Bind<IMessageExpertUserRepository>().To<MessageExpertUserRepository>().InRequestScope();
            kernel.Bind<IMessageCompanyUserService>().To<MessageCompanyUserService>().InRequestScope();
            kernel.Bind<IMessageCompanyUserRepository>().To<MessageCompanyUserRepository>().InRequestScope();
            kernel.Bind<IMessagePositionUserService>().To<MessagePositionUserService>().InRequestScope();
            kernel.Bind<IMessagePositionUserRepository>().To<MessagePositionUserRepository>().InRequestScope();
            kernel.Bind<IExpertListService>().To<ExpertListService>().InRequestScope();
            kernel.Bind<IExpertListRepository>().To<ExpertListRepository>().InRequestScope();
            kernel.Bind<ICompanyListService>().To<CompanyListService>().InRequestScope();
            kernel.Bind<ICompanyListRepository>().To<CompanyListRepository>().InRequestScope();
            kernel.Bind<IPositionListService>().To<PositionListService>().InRequestScope();
            kernel.Bind<IPositionListRepository>().To<PositionListRepository>().InRequestScope();
            kernel.Bind<ITranslationService>().To<TranslationService>().InRequestScope();
            kernel.Bind<ITranslationRepository>().To<TranslationRepository>().InRequestScope();
            
        }        
    }
}
